function View() {

}

View.prototype.run = function () {
    var self = this;
    self.VideoViewTreeEvent();
};


// 查看录像
View.prototype.VideoViewTreeEvent = function () {
    layui.use(['tree', 'form', 'layer', 'laydate'], function () {
        var tree = layui.tree,
            form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer;

        var devid = ''; // 定义全局变量
        $.ajax({
            type: 'get',
            url: '/video/rec_play1/',
            data: {},
            success: function (result) {
                if (result['code'] === 200) {
                    var json_data = result['data'];
                    layui.tree({
                        elem: '#city-tree', //传入元素选择器
                        nodes: json_data,
                        spread: true,
                        skin: 'demo',
                        //　树节点 - 点击
                        click: function (node) {
                            console.log(node);
                            try {
                                var cameraid = node['cameraid'];
                                if (!cameraid){
                                    $.ajax({
                                        type: 'get',
                                        url: '/video/rec_play1/',
                                        data: {},
                                        success: function (result) {
                                            if (result['code'] === 200) {
                                                try {
                                                    var rtmp_url = node['rtmp_url']; // 获取播放地址
                                                    console.log(rtmp_url);
                                                } catch (e) {
                                                    layer.alert('摄像头未开启!');
                                                    return
                                                }
                                                var player = videojs('player');
                                                player.src({
                                                    type: "rtmp/flv",
                                                    src: rtmp_url
                                                });
                                                player.load(rtmp_url);
                                                player.play();
                                            }
                                        },
                                        error: function () {
                                            layer.alert('视频流获取失败，请稍后重试...');
                                        }
                                    });
                                }else {
                                    devid = cameraid; // 赋值给全局变量
                                }
                            }catch (e) {

                            }
                        }
                    });
                }
            }
        });


                //视频录像操作逻辑 加载日期时间控件
        laydate.render({
            elem: '#video-date1',
            type: 'datetime',
            range: true,
            format: 'yyyyMMddHHmmss',
            done: function (value, data) {
                console.log(devid);
                $.ajax({
                    type: 'get',
                    url: '/video/search_video/',
                    data: {
                        'date_value': value,
                        'dev_id': devid
                    },
                    success: function (result) {
                        if (result['code'] === 200) {
                            var rtmp_url = result['data']['rtmp_url'];
                            console.log(rtmp_url);
                            try {
                            } catch (e) {
                                layer.alert('摄像头未开启!');
                                return
                            }

                            var player = videojs('player');
                            player.src({
                                // type: "rtmp/flv",
                                type: 'application/x-mpegURL',
                                src: rtmp_url
                            });
                            player.load(rtmp_url);
                            player.play();

                        } else {
                            layer.alert('请先选择监控点.');
                        }
                    }
                });
            }
        });

        laydate.render({
            elem: '#video-date2',
            type: 'datetime',
            done: function (value, data) {
                console.log(value);
                console.log(data);
            }
        });

        // 点击生成预警单逻辑
        var addWarn = $('#add-warn');
        addWarn.click(function () {
            $.ajax({
                type: 'post',
                url: '/public/rec_data',
                data: {
                    'dev_id': devid,
                    'warn_type': 2
                },
                success: function (result) {
                    if (result['code'] === 200) {
                        layer.alert('预警单创建成功,请稍后查看.');
                    } else {
                        layer.alert('获取数据失败，请稍后重试...');
                    }
                },
                error: function () {
                    layer.alert('获取数据失败，请先点击监控点...');
                }
            });
        });

        // video页面点击查看
        var videoDemoBtn = $('#video-demo');
        videoDemoBtn.click(function () {
            layer.confirm('点击确认，跳转处理页.', {
                btn: '确认',
                yes: function (index, layero) {
                    layer.close(index);
                    setTimeout("window.location.href='/warn_list/'"); //　跳转
                }
            });
        });
    });
};


// 格式化播放录像的时间片段
$(function () {
    if (window.template) {
        template.defaults.imports.timeSince = function (dateValue) {
            // return new Date(parseInt(dateValue) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
            var times = new Date(parseInt(dateValue) * 1000);
            var hour = times.getHours();
            var minute = times.getMinutes();
            var second = times.getSeconds();
            return hour + ":" + minute + ":" + second;
        }
    }
});


$(function () {
    var view = new View();
    view.run();
});